/*
 * @lc app=leetcode.cn id=103 lang=javascript
 *
 * [103] 二叉树的锯齿形层序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @return {number[][]}
 */
var zigzagLevelOrder = function (root) {
  if (!root) {
    return [];
  }
  const res = [];
  const queue = [];
  queue.push({ node: root, level: 0 })
  while (queue.length > 0) {
    const { node, level } = queue.shift();
    console.log(node, level)
    res[level] = res[level] ? res[level].concat(node.val) : [node.val]
    const left = node.left;
    const right = node.right;
    left && queue.push({ node: left, level: level + 1 });
    right && queue.push({ node: right, level: level + 1 });
  }
  return res.map((item, index) => index % 2 === 1 ? item.reverse() : item);
};
// @lc code=end

